package cn.cqone.crm.controller;

import cn.cqone.crm.domain.Dictionary;
import cn.cqone.crm.domain.DictoryDetails;
import cn.cqone.crm.query.DictoryDetailsQuery;
import cn.cqone.crm.service.DictionaryService;
import cn.cqone.crm.service.DictoryDetailsService;
import cn.cqone.crm.utils.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@RequestMapping("/dictoryDetails")
@CrossOrigin
@RequiresPermissions("systemdictionary:main")
public class DictoryDetailsController extends BaseController<DictoryDetails,Long, DictoryDetailsQuery>{
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private DictoryDetailsService dictoryDetailsService;
    //联表新增
    @RequestMapping(value = "/insert/{dictionary_id}",method = RequestMethod.POST)
    public JSONResult insert(@RequestBody DictoryDetails dictoryDetails,
                             @PathVariable("dictionary_id") Long dictionary){
        try{
            Dictionary dictionary1 = dictionaryService.findById(dictionary);
            dictoryDetails.setDictionary(dictionary1);
            dictoryDetailsService.add(dictoryDetails);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("添加失败 请仔细检查");
        }
    }
    //联表修改
    @RequestMapping(value = "/updateAll/{dictionary_id}",method = RequestMethod.POST)
    public JSONResult updateAll(@RequestBody DictoryDetails dictoryDetails,
                             @PathVariable("dictionary_id") Long dictionary){
        try{
            Dictionary dictionary1 = dictionaryService.findById(dictionary);
            dictoryDetails.setDictionary(dictionary1);
            dictoryDetailsService.update(dictoryDetails);
            DictoryDetails id = dictoryDetailsService.findById(dictoryDetails.getId());
            return JSONResult.ok(id);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("添加失败 请仔细检查");
        }
    }
}
